$(function() {
	FeedBackService.loadNoReplyUserList();
	$(window).keydown(function(event) {
		if(event.keyCode == 13) {
			replyUserFeedBack();
		}
	});
	
	$(".btn-sent").click(replyUserFeedBack);
	$(".btn-mark").click(markUserFeedBack);
	
	$(".item-tab div").click(function() {
		if(!$(this).hasClass("cur")) {
			$(".item-tab div").removeClass("cur");
			$(this).addClass("cur");
			if($(this).hasClass("tab1")) {
				FeedBackService.loadNoReplyUserList();
			} else {
				FeedBackService.loadRepliedUserList();
			}
		}
	});
});
var FeedBackService = {
	loadNoReplyUserList : function() {
		$.post("feed_back_user_list.htm",{status:"0"}, function(data){
			if(data && data.length > 0) {
				$(".item").remove();
				for(var i in data){
					var list = $("<li class='item' rel='" + data[i].user_id + "'></li>");
					list.append("<img src='static/imgs/userhead.png'>");
					if(data[i].true_name) {
						list.append("<h2 class='user_id'>" + data[i].true_name + "</h2>");
					} else {
						list.append("<h2 class='user_id'></h2>");
					}
					if(data[i].mobile) {
						list.append("<h4 class='user_phone'>" + data[i].mobile + "</h4>");
					} else {
						list.append("<h4 class='user_phone'></h4>");
					}
					if(data[i].no_reply_count > 0) {
						list.append("<span class='no_read_message'>" + data[i].no_reply_count + "</span>");
					}
					$(".user-list").append(list);
					$(".item").eq(0).click();
				}
				
				$(".item").eq(0).addClass("active");
				$(".user-list .item:first").css("margin-top", "30px");
	    		$(".title").text($(".active").find("h2").text());
	    		FeedBackService.loadChatList(data[0].user_id);
				// 为.item绑定事件：点击左侧列表   添加背景颜色及右侧显示聊天记录
		    	$(".item").click(toLoadChatList);
		    	//调整用户列表滚动条
				$(".user-list").scrollTop(0);
			}
		},"json");
	},
	loadRepliedUserList : function() {
		$.post("feed_back_user_list.htm",{status:"1"}, function(data){
			if(data && data.length > 0) {
				$(".item").remove();
				for(var i in data){
					var list = $("<li class='item' rel='" + data[i].user_id + "'></li>");
					list.append("<img src='static/imgs/userhead.png'>");
					if(data[i].true_name) {
						list.append("<h2 class='user_id'>" + data[i].true_name + "</h2>");
					} else {
						list.append("<h2 class='user_id'></h2>");
					}
					if(data[i].mobile) {
						list.append("<h4 class='user_phone'>" + data[i].mobile + "</h4>");
					} else {
						list.append("<h4 class='user_phone'></h4>");
					}
					if(data[i].no_reply_count > 0) {
						list.append("<span class='no_read_message'>" + data[i].no_reply_count + "</span>");
					}
					$(".user-list").append(list);
					$(".item").eq(0).click();
				}
				
				$(".item").eq(0).addClass("active");
				$(".user-list .item:first").css("margin-top", "30px");
	    		$(".title").text($(".active").find("h2").text());
	    		FeedBackService.loadChatList(data[0].user_id);
				// 为.item绑定事件：点击左侧列表   添加背景颜色及右侧显示聊天记录
		    	$(".item").click(toLoadChatList);
		    	//调整用户列表滚动条
				$(".user-list").scrollTop(0);
			}
		},"json");
	},
	loadChatList: function(userId) {
		var param = {
			t : new Date(),
			userId : userId
		};
		$.post("user_chat_list.htm", param, function(data){
			if(data) {
				$(".chat-con-wrap").html("");
				$(".chat-con-wrap").scrollTop(0);
				for(var i in data){
					var html = '';
					if(data[i].content && data[i].content.trim()) {
						if(data[i].gmt_create && i == 0) {
							html += '<div class="message-time clearfix" rel="' + data[i].gmt_create + '" style="text-align: center;margin: 5px auto;">' + new Date(data[i].gmt_create).toCNString() + '</div>';
						} else if(data[i].gmt_create && data[i - 1].gmt_create && Math.floor(data[i].gmt_create/86400000) - Math.floor(data[i - 1].gmt_create/86400000) > 0) {
							html += '<div class="message-time clearfix" rel="' + data[i].gmt_create + '" style="text-align: center;margin: 5px auto;">' + new Date(data[i].gmt_create).toCNString() + '</div>';
						} else if(data[i].gmt_create && data[i - 1].gmt_create && data[i].gmt_create - data[i - 1].gmt_create > 300000) {
							html += '<div class="message-time clearfix" rel="' + data[i].gmt_create + '" style="text-align: center;margin: 5px auto;">' + new Date(data[i].gmt_create).toShortTime() + '</div>';
						} else {
							html += '<div class="message-time clearfix" rel="' + data[i].gmt_create + '" style="text-align: center;margin: 5px auto;"></div>';
						}
						
						if(data[i].type && data[i].type == "feed") {
							html += '<div class="customer clearfix" rel="' + data[i].id + '" style="float: left; margin: 1%; width: 98%;">' +
										'<table>' +
											'<tr>' +
												'<td style="vertical-align: top"><img src="static/imgs/userhead.png" style="width: 30px;height: 30px;"></td>' +
												'<td><div class="feedback" style="">' + data[i].content + '</div></td>' +
											'</tr>' +
										'</table>' +
									'</div>';
							if(i == data.length - 1 && data[i].status == "1") {
								html += '<div class="custom_service clearfix" style="float: right; margin: 1%; width: 98%;">' +
											'<table style="float: right;">' +
												'<tr>' +
													'<td><div class="feedbackreply" style="">标记为已回复</div></td>' +
													'<td style="vertical-align: top"><img src="static/imgs/service.png" style="width: 30px;height: 30px;"></td>' +
												'</tr>' +
											'</table>' +
										'</div>';
							}
						} else if(data[i].type && data[i].type == "reply") {
							html += '<div class="custom_service clearfix" style="float: right; margin: 1%; width: 98%;">' +
										'<table style="float: right;">' +
											'<tr>' +
												'<td><div class="feedbackreply" style="">' + data[i].content + '</div></td>' +
												'<td style="vertical-align: top"><img src="static/imgs/service.png" style="width: 30px;height: 30px;"></td>' +
											'</tr>' +
										'</table>' +
									'</div>';
						}
						//显示客服标记已回复
						
					}
					$(".chat-con-wrap").append(html);
				}
				$(".chat-con-wrap").scrollTop($(".chat-con-wrap .clearfix:last").position().top);
			}
		},"json");
	}

};

function toLoadChatList() {
	$(".title").text($(this).find("h2").text());
	$(this).addClass("active").siblings().removeClass("active");
	$(this).addClass("prevent").siblings().removeClass("prevent");
	FeedBackService.loadChatList($(this).attr("rel"));
}

function replyUserFeedBack() {
	var param = {
		t : new Date(),
		userId : $(".active").attr("rel"),
		feedBackId : $(".chat-con-wrap .customer:last").attr("rel"),
		replyContent : $(".btn-sent").siblings().find("textarea").val()
	};
	if(!param.userId) {
		return ;
	}
	if(!param.replyContent){
		$("#replyContent").focus();
		return ;
	}
	$.post("reply_user_feed_back.htm", param, function(data){
		if(data.code == "success") {
			$(".btn-sent").siblings().find("textarea").val("");
			$(".active").find(".no_read_message").remove();
			FeedBackService.loadChatList(param.userId);
		} else {
			alert(data.msg);
		}
	},"json");
}

function markUserFeedBack() {
	var param = {
		t : new Date(),
		userId : $(".active").attr("rel")
	};
	if(!param.userId) {
		return ;
	}
	$.post("mark_user_feed_back.htm", param, function(data){
		if(data.code == "success") {
			$(".btn-sent").siblings().find("textarea").val("");
			$(".active").find(".no_read_message").remove();
			FeedBackService.loadChatList(param.userId);
		} else {
			alert(data.msg);
		}
	},"json");
}

Date.prototype.toCNString = function() {
	return this.getFullYear() + "-" + (this.getMonth()>=9 ? this.getMonth() + 1 : "0" + (this.getMonth() + 1)) + "-" + (this.getDate()>9 ? this.getDate() : "0" + this.getDate()) + " " + (this.getHours()>9 ? this.getHours() : "0" + this.getHours()) + ":" + (this.getMinutes()>9 ? this.getMinutes() : "0" + this.getMinutes()) + ":" + (this.getSeconds()>9 ? this.getSeconds() : "0" + this.getSeconds());
};
Date.prototype.toShortTime = function() {
	return (this.getHours()>9 ? this.getHours() : "0" + this.getHours()) + ":" + (this.getMinutes()>9 ? this.getMinutes() : "0" + this.getMinutes());
};